package com.carhailing.coupons.config;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ClassName:
 * @Description: 消息发送到队列的确认
 * @Author: zhuwenqiang
 * @Date: 2023/10/23
 */
@Component
public class ReturnsCallbackConfig implements RabbitTemplate.ReturnsCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        rabbitTemplate.setReturnsCallback(this);
    }

    /**
     * 只有发送到队列失败才会执行
     *
     * @param returnedMessage the returned message and metadata.
     */
    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        System.out.println("消息" + returnedMessage.getMessage().toString() +
                "被交换机" + returnedMessage.getExchange() + "回退！" +
                "退回原因为：" + returnedMessage.getReplyText());
        // TODO 补偿 可以再发 做日志记录
    }

}
